package com.rzj.book.controller;

import com.rzj.book.en.User;
import com.rzj.book.service.IUserService;
import com.rzj.book.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description TODO
 * @Author Administrator
 * @Date 2020/5/16 21:25
 * @Version 1.0
 */
@Controller
@RequestMapping("/userLogin")
public class LoginController extends BaseController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/userLogin")
    public String userLogin(String code,User user){
        HttpSession session = getSession();
        System.out.println("login"+session.getId());
        String sessionCode = (String)session.getAttribute("checkcode");
        if(!sessionCode.equalsIgnoreCase(code)){
            ajaxReturn(false, "验证码错误");
        }
        User existUser = userService.login(user.getUserName(),user.getPwd());
// 判断
        if (existUser == null) {
            // 登录失败
         ajaxReturn(false,"账号或者密码错误!");
            return "error";
        } else {
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("companyId",existUser.getTel());
            map.put("companyName",existUser.getAddress());
            // 登录成功
            // 将用户的信息存入到session中
            session.setAttribute("existUser", existUser);
            // 页面跳转
            return "index";
        }
    }

}
